package com.globallogic.vacationplanner.web.helper;

import org.apache.log4j.Logger;

import com.globallogic.vacationplanner.transfer.dto.impl.UserDto;
import com.globallogic.vacationplanner.web.controller.UserBean;
import com.globallogic.vacationplanner.web.exceptions.ViewException;
import com.globallogic.vacationplanner.web.utils.HashGenerator;

public class UserBeanHelper {
	private static Logger LOGGER = Logger.getLogger(UserBeanHelper.class);

	public UserDto buildUserDto(UserBean userBean) throws ViewException {
		LOGGER.info("start build UserDto from project backing bean");
		UserDto userDto = new UserDto();
		userDto.setLogin(userBean.getLogin());

		if (!userBean.getPassword().equalsIgnoreCase(userBean.getConfirmPassword()) && userBean.getConfirmPassword()!=null) {
			throw new ViewException();
		}
		LOGGER.info("start to encrypt passwords");
		String cryptPassword = HashGenerator.getMD5Hash(userBean.getPassword());
		if (userBean.getConfirmPassword() != null) {
			String cryptConfirmPassword = HashGenerator.getMD5Hash(userBean
					.getConfirmPassword());
			userDto.setConfirmPassword(cryptConfirmPassword);
		} else {
			userDto.setConfirmPassword(userBean.getConfirmPassword());
		}
		LOGGER.info("pasword has been encrypded success");
		userDto.setPassword(cryptPassword);
		userDto.setUserName(userBean.getUserName());
		userDto.setSurname(userBean.getSurname());
		userDto.setEmail(userBean.getEmail());
		LOGGER.info("build UserDto was success");
		return userDto;
	}
}
